<template>
  <div class="_form">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="名字" prop="name">
        <el-input styel="width:140px" v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="专业" prop="major">
        <el-input v-model="ruleForm.major"></el-input>
      </el-form-item>
      <el-form-item label="成绩" prop="grade">
        <el-input v-model="ruleForm.grade"></el-input>
      </el-form-item>
      <el-form-item label="班级" prop="student_class">
        <el-input v-model="ruleForm.student_class"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="ruleForm.address"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >添加</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'AddStudent',
  data() {
    let checkGrade = (rule, value, callback) => {
      setTimeout(() => {
        if (isNaN(value)) {
          callback(new Error('请输入数字值'))
        } else {
          callback()
        }
      }, 1000)
    }
    return {
      ruleForm: {
        id: null,
        name: '',
        major: '',
        grade: null,
        student_class: '',
        address: null,
        phone: null,
        email: null,
      },
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        major: [{ required: true, message: '请输入专业', trigger: 'blur' }],

        student_class: [
          { required: true, message: '请输入班级', trigger: 'blur' },
        ],
        grade: [{ validator: checkGrade, trigger: 'blur' }],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitData(this.ruleForm)
          this.$message.success('添加成功!')
        } else {
          this.$message.error('检查一下!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },

    submitData(data) {
      this.$axios
        .post('/addStudent', data, {
          headers: { 'content-type': 'application/json' },
        })
        .then(
          function (response) {
            console.log(response)
          },
          function (err) {
            console.log(err)
          }
        )
    },
  },
}
</script>
<style scoped>
._form/deep/ .el-input {
  width: 40%;
}
</style>
